package myutils
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.net.FileReference;
	import flash.net.FileReferenceList;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;
	import flash.system.LoaderContext;
	import flash.utils.Dictionary;

	public class LoadManager
	{
		private static var _ins:LoadManager 
		private var _dic:Dictionary = new Dictionary();
		private var arr:Array = new Array();
		private var INDEX:int;
		private var bool:Boolean;
		
		public static function getInstance():LoadManager
		{
			if(_ins == null)
			{
				_ins = new LoadManager(); 
			}
			return _ins;
		}
		
		public function LoadManager()
		{
			var request:URLRequest = new URLRequest("../resource.xml");
			var loader:URLLoader = new URLLoader(request);
			loader.addEventListener(Event.COMPLETE, onResourceComplete);
		}
		
		private function onResourceComplete(event:Event):void
		{
			var loader:URLLoader = event.target as URLLoader;
			if(loader)
			{
				var res:XML = new XML(loader.data);
				for each (var str:XML in res.image.loader)
				{
					arr.push(str.@res);					
				}
				loadResource(0);
			}
		}
		
		private function loadResource(i:int):void
		{
			var ldr:Loader = new Loader();
			ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onDeal);
			ldr.load(new URLRequest(arr[i]));
		}
		
		
		private function onDeal(e:Event):void
		{
			INDEX++;
			var loadinfo:LoaderInfo = e.target as LoaderInfo
			
			var bitmapdata:BitmapData = (loadinfo.content as Bitmap).bitmapData
		
			var str:String = loadinfo.url.substring(loadinfo.url.lastIndexOf("/")+1)
				
			_dic[str] = bitmapdata; 
			if(INDEX == arr.length)
			{
				//加载资源完成。
				bool = true;
//				Demo.getInstance().onLoadComplete();
				
				GameStart.gameStart.onLoadComplete();
				
			}
			else
			{
				loadResource(INDEX);
			}
		}
		
		
		public function getBitmapData(url:String):BitmapData
		{
			if(bool)
				return _dic[url];
			else 
				return null;
		}
		
		
		
	}
}